package com.chenhy.server.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chenhy.server.mapper.DepartmentMapper;
import com.chenhy.server.pojo.Department;
import com.chenhy.server.pojo.RespBean;
import com.chenhy.server.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author gangan
 * @since 2022-02-13
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getAllDepartment() {
        return departmentMapper.getAllDepartmentByParentId(-1);
    }

    @Override
    public RespBean addDepartment(Department department) {
        department.setEnabled(true);
        // 触发储存过程结果会被传入对象接收
        departmentMapper.addDepartment(department);
        if (1 == department.getResult()){
            return RespBean.success("添加成功", department);
        }
        return RespBean.error("添加失败");
    }

    @Override
    public RespBean deleteDepartment(Integer id) {
        Department department = new Department();
        department.setId(id);
        departmentMapper.deleteDepartment(department);
        if (department.getResult() == 1){
            return RespBean.success("删除成功",department);
        } else if (department.getResult() == -1){
            return RespBean.error("删除失败，该部门存在下属员工",department);
        } else if (department.getResult() == -2){
            return RespBean.error("删除失败，该部门存在子部门",department);
        }
        return RespBean.error("删除失败");
    }

}
